/*
 * alu.h
 *
 *  Created on: Feb 4, 2012
 *      Author: Craig Markham
 *              Class:  TCSS 371
 *              Assignment:
 */

#ifndef ALU_H_
#define ALU_H_


#endif /* ALU_H_ */
#include <time.h>
#include "registerFile.h"


/*
 * =======================================================================================
 * ALU implementation
 * =======================================================================================
 */

//ALU opcode definitions
#define OVERLOAD_OP     0x9
#define SHL_OP          0x8
#define SHR_OP          0xA
#define ADD_OP          0x0
#define SUB_OP          0x1
#define MUL_OP          0x2
#define DIV_OP          0x3
#define AND_OP          0x4
#define OR_OP           0x5
#define XOR_OP          0x6
#define NOT_OP          0x7

//ALU ADT
typedef struct {
  Register register_a;
  Register register_b;
  Register register_r;
  Register SW;
  int negative_flag;
  int zero_flag;
  int carry_flag;
  int overflow_flag;
  int positive_flag;
  int primary_op;
  int overload_op;
} ALU;

//ALU function prototypes

ALU * alu_init(ALU *);

void alu_clearFlags(ALU *);

void alu_setSWReference(ALU *, Register);

void alu_setFlags(ALU *, int);

void alu_putOperandA(ALU *, Register);

void alu_putOperandB(ALU *, Register);

void alu_getOperation(ALU *, int, int);

void alu_perform_operation_primary(ALU *, int *);

void alu_perform_operation_secondary(ALU *, int *);

void alu_add(ALU *, int *);

void alu_sub(ALU *, int *);

void alu_mul(ALU *, int *);

void alu_div(ALU *, int *);

void alu_and(ALU *, int *);

void alu_or(ALU *, int *);

void alu_xor(ALU *, int *);

void alu_not(ALU *, int *);

void alu_shl(ALU *, int *);

void alu_shr(ALU *, int *);


/*
 * =======================================================================================
 * Memory implementation
 * =======================================================================================
 * Address space is 64k (216) bytes. A 16-bit word consists of two consecutive bytes with
 * the high order byte (hob) at the lower memory address and the low order byte (lob) at
 * the next memory address (big endian)
 */

#define MEMORY_SIZE 1024

typedef uchar * MemoryModulePtr;

MemoryModulePtr memory_initialize(); // creates user memory space and fills with random numbers

uchar memory_fetchByteFrom(MemoryModulePtr, ushort, int *);
// returns a byte form address, sets error code
int memory_storeByteTo(MemoryModulePtr, ushort, uchar); // stores byte
// at address, sets error code
ushort memory_fetchWordFrom(MemoryModulePtr, ushort, int *); // returns
// the contents of two consecutive locations
//starting at address. sets error code
ushort memory_storeWordTo(MemoryModulePtr, ushort, ushort); // stores
// the contents at two consecutive locations
// starting at address. returns error code

